<script setup lang="ts">
import { createRoleUserAPI, getRoleDetailAPI, getTreeListAPI } from '@/apis/system';
import type { RoleData, RoleParams } from '@/types/system';
import { ElMessage, type ElTree } from 'element-plus';
import { computed, onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';


const activeStep = ref(0)
const decreseStep = () => {
    if (activeStep.value === 0) return
    activeStep.value--
}

// 角色信息表单
const roleFormRef = ref()

const roleForm = ref<{
    roleName?: string
    remark?: string
    perms?: object[]
}>({
    roleName: '',
    remark: '',
    // 选中的权限
    perms: []
})
const roleRules = ref({
    roleName: [
        { required: true, message: '请输入角色名称', trigger: 'blur' }
    ]
})


// 权限列表
const treeList = ref<RoleData[]>([]) // 权限树形列表
const getTreeList = async () => {
    const res = await getTreeListAPI()
    treeList.value = res.data
}
getTreeList()

const editTreeRef = ref<InstanceType<typeof ElTree>[]>()
const diabledTreeRef = ref<InstanceType<typeof ElTree>[]>()
const increseStep = async () => {
    if (activeStep.value === 2) return
    if (activeStep.value === 0) {
        // 当前是角色信息状态，进行表单校验
        await roleFormRef.value.validate()
        // 校验通过才能进入下一个状态
        activeStep.value++
    } else if (activeStep.value === 1) {
        // 当前是权限信息状态（每次默认重置为空，避免上一步返回数据累加）
        roleForm.value.perms = []
        editTreeRef.value!.forEach((tree: InstanceType<typeof ElTree>) => {
            roleForm.value?.perms?.push(tree.getCheckedKeys())
        })
        if (roleForm.value.perms.flat().length === 0) {
            ElMessage.error('请至少选择一个权限点！')
        } else {
            // 进入下一页
            activeStep.value++
            // 回填已选择数据
            diabledTreeRef.value!.forEach((tree, index) => {
                // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                //@ts-ignore  
                tree.setCheckedKeys(roleForm.value.perms[index])
            })
        }
    }
}


// 0. 编辑缓存roleId
const route = useRoute()
const roldId = computed(() => {
  return route.query.id as string
})
onMounted(() => {
  // 如果有id，就是编辑角色
  if (roldId.value) {
    editRoleDetail()
  }
})

const editRoleDetail = async () => {
  const res = await getRoleDetailAPI(Number(roldId.value))
  console.log(res.data);
  
  const { perms, remark, roleId, roleName } = res.data
  // 回填基础表单
  roleForm.value = { remark, roleName }
  const arr = perms.flat()
  arr.forEach(item => {
    roleForm.value.perms?.push(new Number(item))
  })
  // 回填Tree
  editTreeRef.value!.forEach((tree, index) => {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    //@ts-expect-error
    tree.setCheckedKeys(roleForm.value.perms[index])
  })
}

const router = useRouter()
const confirmAdd = async ()=>{
  await createRoleUserAPI(
    roleForm.value as RoleParams
  )
  ElMessage.success('添加角色成功~~')
  router.back()
}

</script>

<template>
    <div class="add-role">
        <header class="add-header">
            <el-page-header content="添加角色" @back="$router.back()" />
        </header>
        <main class="add-main">
            <div class="step-container">
                <el-steps direction="vertical" :active="activeStep">
                    <el-step title="角色信息" />
                    <el-step title="权限信息" />
                    <el-step title="检查并完成" />
                </el-steps>
            </div>
            <div v-show="activeStep === 0" class="form-container">
                <div class="title">角色信息</div>
                <div class="form">
                    <el-form ref="roleFormRef" class="form-box" :model="roleForm" :rules="roleRules">
                        <el-form-item label="角色名称" prop="roleName">
                            <el-input v-model="roleForm.roleName" />
                        </el-form-item>
                        <el-form-item label="角色描述">
                            <el-input v-model="roleForm.remark" />
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div v-show="activeStep === 1" class="form-container">
                <div class="title">权限配置</div>
                <div class="form">
                    <div class="tree-wrapper">
                        <div v-for="item in treeList" :key="item.id" class="tree-item">
                            <div class="tree-title">{{ item.title }}</div>
                            <el-tree ref="editTreeRef" :data="item.children" show-checkbox default-expand-all
                                node-key="id" highlight-current :props="{ label: 'title' }" />
                        </div>
                    </div>
                </div>
            </div>
            <div v-show="activeStep === 2" class="form-container">
                <div class="title">检查并完成</div>
                <div class="form">
                    <div class="info">
                        <div class="form-item">角色名称：{{ roleForm.roleName }}</div>
                        <div class="form-item">角色描述：{{ roleForm.remark }}</div>
                        <div class="form-item">角色权限：</div>
                        <div class="tree-wrapper">
                            <div v-for="item in treeList" :key="item.id" class="tree-item">
                                <div class="tree-title">{{ item.title }}</div>
                                <el-tree ref="diabledTreeRef" :data="item.children" show-checkbox default-expand-all
                                    node-key="id" :highlight-current="false"
                                    :props="{ label: 'title', disabled: () => true }" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <footer class="add-footer">
            <div class="btn-container">
                <el-button v-if="activeStep > 0" @click="decreseStep">上一步</el-button>
                <el-button v-if="activeStep < 2" type="primary" @click="increseStep">下一步</el-button>
                <el-button v-if="activeStep === 2" type="primary" @click="confirmAdd">确认添加</el-button>
            </div>
        </footer>
    </div>
</template>

<style scoped lang="scss">
.add-role {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 20px;
        height: 64px;
        line-height: 64px;
        font-size: 16px;
        font-weight: bold;
        background-color: #fff;

        .left {
            span {
                margin-right: 4px;
            }

            .arrow {
                cursor: pointer;
            }
        }

        .right {
            text-align: right;
        }
    }

    .add-main {
        position: fixed;
        top: 64px;
        bottom: 88px;
        width: 100%;
        overflow-y: auto;
        background: #f4f6f8;
        padding: 20px 30px;
        display: flex;

        .step-container {
            height: 400px;
            width: 200px;
        }

        .form-container {
            flex: 1;
            background-color: #fff;
            overflow-y: auto;

            .title {
                height: 60px;
                line-height: 60px;
                padding-left: 20px;
            }

            .form {
                margin-bottom: 20px;
                padding: 20px 65px 24px;

                .el-form {
                    display: flex;
                    flex-wrap: wrap;
                }

                .info {
                    font-size: 14px;
                    color: #666;

                    .form-item {
                        margin-bottom: 20px;
                    }
                }
            }

            .form-box {
                width: 600px;
                display: flex;
                flex-direction: column;
            }
        }
    }

    .add-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
        padding: 24px 50px;
        color: #000000d9;
        font-size: 14px;
        background: #fff;
        text-align: center;
        z-index: 10001;

        .btn-container {
            width: 200px;
            margin: 0 auto;
            transform: translateX(-50%)
        }
    }

    .tree-wrapper {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;

        .tree-item {
            flex: 1;
            padding: 0px 4px;
            text-align: center;

            .tree-title {
                background: #f5f7fa;
                text-align: center;
                padding: 10px 0;
                margin-bottom: 12px;
            }
        }
    }
}
</style>